#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fileencoding=utf-8
# vim:tabstop=4
#
# Copyright (c) 2010 by xkhhdx
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

__serviceprovider__ = "follow5"

__properties__ = {
    "username" : u"用户名",
    "password" : u"密码",
}

__api__ = ""

__lengthlimit__ = 140

import base64, urllib, urllib2

def update(properties, msg):
    username = properties["username"]
    password = properties["password"]

    auth=base64.b64encode(username+":"+password)
    auth='Basic '+auth

    form_fields = {
            "status" : msg,
    }
    form_data = urllib.urlencode(form_fields)

    url="http://api.follow5.com/api/statuses/update.xml?api_key=%(api)s" % {"api":__api__}
    #print url
    headers = {
        "User-Agent" : "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)",
        "Authorization" : auth
    }

    try:
        req = urllib2.Request(url, form_data, headers)
        response = urllib2.urlopen(req)
        result = response.read()

    except Exception,e:
        return True, e
    else:

        if result.find("<status/>") < 0:
            return False, "succeed!"
        else:
            return True, "Failed!"

if __name__ == "__main__":
    properties = __properties__

    properties["username"] = ""
    properties["password"] = ""

    s,r = update(properties, u"hello world你好")
    print "s,r=", s, r

